package com.andy.blog.quartz.controller;

import com.andy.blog.common.annotation.Log;
import com.andy.blog.common.enums.BusinessTypeEnum;
import com.andy.blog.common.exception.job.TaskException;
import com.andy.blog.common.page.PageUtilsDTO;
import com.andy.blog.common.reponse.GeneralResponseResult;
import com.andy.blog.common.utils.EasyExcelUtils;
import com.andy.blog.common.utils.TransformUtils;
import com.andy.blog.dal.vo.SysJobVO;
import com.andy.blog.quartz.dto.SysJobDTO;
import com.andy.blog.quartz.service.SysJobBusService;
import org.quartz.SchedulerException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 调度任务信息操作处理
 * Created by andy on 2023-04-05 11:38
 */
@RestController
@RequestMapping("/monitor/job")
public class SysJobController {

    @Resource
    private SysJobBusService sysJobBusService;

    /**
     * 查询定时任务列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:list')")
    @GetMapping("/list")
    public GeneralResponseResult<PageUtilsDTO<SysJobDTO>> list(SysJobVO sysJobVO) {
        return GeneralResponseResult.success(sysJobBusService.selectListByPage(sysJobVO));
    }

    /**
     * 导出定时任务列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:export')")
    @Log(title = "定时任务", businessType = BusinessTypeEnum.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysJobVO sysJobVO) {
        List<SysJobDTO> list = sysJobBusService.selectListByCondition(sysJobVO);
        EasyExcelUtils.writeStyleExcel(response, list, "定时任务", SysJobDTO.class);
    }

    /**
     * 获取定时任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:query')")
    @GetMapping(value = "/{jobId}")
    public GeneralResponseResult<SysJobDTO> getInfo(@PathVariable("jobId") Long jobId) {
        return GeneralResponseResult.success(sysJobBusService.selectById(jobId));
    }

    /**
     * 新增定时任务
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:add')")
    @Log(title = "定时任务", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    public GeneralResponseResult<Void> add(@RequestBody SysJobVO job) throws SchedulerException, TaskException {
        sysJobBusService.insertJob(job);
        return GeneralResponseResult.success();
    }

    /**
     * 修改定时任务
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:edit')")
    @Log(title = "定时任务", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    public GeneralResponseResult<String> edit(@RequestBody SysJobVO job) throws SchedulerException, TaskException {
        sysJobBusService.updateJob(job);
        return GeneralResponseResult.success();
    }

    /**
     * 定时任务状态修改
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:changeStatus')")
    @Log(title = "定时任务", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/changeStatus")
    public GeneralResponseResult<Void> changeStatus(@RequestBody SysJobVO job) throws SchedulerException {
        SysJobDTO newJobDto = sysJobBusService.selectById(job.getId());
        SysJobVO newJob = TransformUtils.transform(newJobDto, SysJobVO.class);
        newJob.setStatus(job.getStatus());
        sysJobBusService.changeStatus(newJob);
        return GeneralResponseResult.success();
    }

    /**
     * 定时任务立即执行一次
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:changeStatus')")
    @Log(title = "定时任务", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/run")
    public GeneralResponseResult<String> run(@RequestBody SysJobVO job) throws SchedulerException {
        boolean result = sysJobBusService.run(job);
        return result ? GeneralResponseResult.success() : GeneralResponseResult.error("任务不存在或已过期！");
    }

    /**
     * 删除定时任务
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:remove')")
    @Log(title = "定时任务", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{jobIds}")
    public GeneralResponseResult<Void> remove(@PathVariable Long[] jobIds) throws SchedulerException, TaskException {
        sysJobBusService.deleteJobByIds(jobIds);
        return GeneralResponseResult.success();
    }
}
